using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using HengTong.ViewModel.Daily.SysDailySubstitutionRequestBillVms;
using HengTong.Model.Db.Hr;
using TiaoTaoHelper.Models;
using TiaoTaoHelper.Wtm.Attributes;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.Areas.Daily.Controllers
{
    [Area("Daily")]
    [AuthorizeJwtWithCookie]
    [ActionDescription("替班申请")]
    [ApiController]
    [Route("api/SysDailySubstitutionRequestBill")]
    public partial class SysDailySubstitutionRequestBillController : BaseApiController
    {
        [ActionDescription("Sys.Search")]
        [HttpPost("Search")]
        public IActionResult Search(SysDailySubstitutionRequestBillSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillListVm>(passInit: true);
                vm.Searcher = searcher;
                return Ok(vm.GetJsonForApi());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public SysDailySubstitutionRequestBillVm Get(string id)
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillVm>(id);
            return vm;
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("Add")]
        [PreventDuplicateSubmit]
        public async Task<IActionResult> Add(SysDailySubstitutionRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }

        [ActionDescription("Sys.Edit")]
        [PreventDuplicateSubmit]
        [HttpPut("Edit")]
        public async Task<IActionResult> Edit(SysDailySubstitutionRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }

        [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        [PreventDuplicateSubmit]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillBatchVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }

            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }


        [ActionDescription("Sys.Export")]
        [HttpPost("ExportExcel")]
        public IActionResult ExportExcel(SysDailySubstitutionRequestBillSearcher searcher)
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillListVm>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("ExportExcelByIds")]
        public IActionResult ExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillListVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }

            return vm.GetExportData();
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillImportVm>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }

            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(SysDailySubstitutionRequestBillImportVm vm)
        {
            if (vm != null && (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData()))
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm?.EntityList?.Count ?? 0);
            }
        }


        [HttpGet("GetSysPersons")]
        public ActionResult GetSysPersons()
        {
            return Ok(DC.Set<SysPerson>().GetSelectListItems(Wtm, x => x.Name));
        }

        [HttpGet("GetSysGroups")]
        public ActionResult GetSysGroups()
        {
            return Ok(DC.Set<SysGroup>().GetSelectListItems(Wtm, x => x.GroupName));
        }

        [HttpGet("GetSysOrgs")]
        public ActionResult GetSysOrgs()
        {
            return Ok(DC.Set<SysOrg>().GetSelectListItems(Wtm, x => x.Name));
        }

        [ActionDescription("预测编码")]
        [HttpGet("PredictCode")]
        public async Task<Result<string>> PredictCode()
        {
            var vm = Wtm.CreateVM<SysDailySubstitutionRequestBillVm>();
            return await vm.PredictCodeAsync();
        }
        

        [HttpPost("[action]")]
        [ActionDescription("流程审批")]
        public async Task<ActionResult> Approve(SysDailySubstitutionRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.ApproveAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
    }
}